package task.manager;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

/**
 * Activity class for creating a new task.
 * 
 * @author Harrison Saylor, Aaron Mead, Marc Huet, Greg Ruskin
 * @version 2
 */
public class TaskCreationActivity extends Activity{
	
	private Button taskSubmit;
	private Button cancelTask;
	private EditText nameBox;
	private EditText locationBox;
	private EditText addressBox;
	//private EditText categoryBox;
	private DatePicker datePicker;
	private TimePicker timePicker;
	
	UserDataFacade userDataFacade;
	
	private Task newTask;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.taskcreationactivity);
        
        userDataFacade = UserDataFacade.getInstance();
        
        taskSubmit = (Button)findViewById(R.id.task_submit);
        cancelTask = (Button)findViewById(R.id.cancel_task);
        nameBox = (EditText)findViewById(R.id.namebox);
        locationBox = (EditText)findViewById(R.id.locationbox);
        addressBox = (EditText)findViewById(R.id.addressbox);
        //categoryBox = (EditText)findViewById(R.id.categorybox);
        datePicker = (DatePicker)findViewById(R.id.datePicker1);
        timePicker = (TimePicker)findViewById(R.id.timePicker1);
        
        
        taskSubmit.setOnClickListener(new OnClickListener(){
        	public void onClick(View v) {
        		String taskName = nameBox.getText().toString();
        		String taskLocation = locationBox.getText().toString();
        		String taskAddress = addressBox.getText().toString();
        		//String category = categoryBox.getText().toString();
        		
        		newTask = new Task(taskName,datePicker.getMonth(),datePicker.getDayOfMonth(),datePicker.getYear(),timePicker.getCurrentHour(),timePicker.getCurrentMinute(),userDataFacade.getActiveCategory(),taskLocation,taskAddress);
        		System.out.println("New Task Title: " + newTask.getTitle());
        		userDataFacade.addTask(newTask);
        		
        		//Intent nextScreen = new Intent(getApplicationContext(), TaskViewActivity.class);
        		//startActivity(nextScreen);
        		//finish();
        		
        		Intent nextScreen = new Intent(getApplicationContext(), TaskViewActivity.class);
        		startActivity(nextScreen);
        		finish();
        	}
        });
        
        cancelTask.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		//Intent nextScreen = new Intent(getApplicationContext(), TaskViewActivity.class);
        		//startActivity(nextScreen);
        		//finish();
        		Intent nextScreen = new Intent(getApplicationContext(), TaskViewActivity.class);
        		startActivity(nextScreen);
        		finish();
        	}
        });
	}
	
	@Override
	public void onPause(){
		super.onPause();
		
		try {
			userDataFacade.saveTaskData();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
	}
	

}
